import Vue from 'vue';
let getevent = () => {
    var Event = new Vue();
    return Event;
}
const vueEvent = getevent();
export {vueEvent};
// 获取UUID
export function getUUID () {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
 
    var uuid = s.join("");
    return uuid;
}
//本地存储
export function getlocal(name) {
    let data = localStorage.getItem(name);
    if (data != null && data != '') {
        try {
            let obj = eval('(' + data + ')');
            return obj;
        } catch (e) {
            return '';
        }
    } else {
        return '';
    }
};
export function getSession(name) {
    let data = sessionStorage.getItem(name);
    if (data != null && data != '') {
        try {
            let obj = eval('(' + data + ')');
            return obj;
        } catch (e) {
            return '';
        }
    } else {
        return '';
    }
};
export function setLocal (key, value) {
    window.localStorage.setItem(key, JSON.stringify(value));
};
export function setSession (key, value) {
    window.sessionStorage.setItem(key, JSON.stringify(value));
};
export function clearOneLocal (key) {
    window.localStorage.removeItem(key);
};
export function clearOneSession (key) {
    window.sessionStorage.removeItem(key);
};
export function clearAllLocal () {
    window.localStorage.clear();
};
export function clearAllSession () {
    window.sessionStorage.clear();
};
// 时间格式化
export function parseTime (time, cFormat) {
    if (arguments.length === 0) {
        return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
            time = parseInt(time)
        }
        if ((typeof time === 'number') && (time.toString().length === 10)) {
            time = time * 1000
        }
        date = new Date(time)
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return time_str
};
export function formatTime(time, option) {
    if (('' + time).length === 10) {
        time = parseInt(time) * 1000
    } else {
        time = +time
    }
    const d = new Date(time)
    const now = Date.now()
  
    const diff = (now - d) / 1000
  
    if (diff < 30) {
        return '刚刚'
    } else if (diff < 3600) {
        // less 1 hour
        return Math.ceil(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
        return Math.ceil(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 2) {
        return '1天前'
    }
    if (option) {
        return parseTime(time, option)
    } else {
        return (
            d.getMonth() +
            1 +
            '月' +
            d.getDate() +
            '日' +
            d.getHours() +
            '时' +
            d.getMinutes() +
            '分'
        )
    }
};
//登录错误提示
export function errortip(info) {
    vueEvent.$message({
        message: info,
        type: 'error',
        duration: '2000'
    });
};
//成功提示
export function successtip(info) {
    vueEvent.$message({
        message: info,
        type: 'success',
        duration: '2000'
    });
};
//警告提示
export function warningtip(info) {
    vueEvent.$message({
        message: info,
        type: 'warning',
        duration: '2000'
    });
};
// 单位格式化
export function numberFormat(value) {
    var param = '';
    var k = 10000,
        sizes = ['', '万', '亿', '万亿'],
        i;
    if(value < k) {
        param = value
    } else {
        i = Math.floor(Math.log(value) / Math.log(k)); 
        param = ((value / Math.pow(k, i))).toFixed(1) + sizes[i];
    }
    return param;
}
// 数据格式化
export function converdata(limit){
    limit = limit * 1;
    var size = "";
    if (limit < 1024) {
        size = limit+'KB';
    } else if (limit < 1024 * 1024){
        size = (limit / 1024).toFixed(1)+'MB';
    } else if (limit < 1024 * 1024 * 1024){
        size = (limit / (1024 * 1024)).toFixed(1)+'GB';
    } else {
        size = (limit / (1024 * 1024 * 1024)).toFixed(1)+'TB';
    }
    return size;
    // var sizestr = size + "";   
    // var len = sizestr.indexOf("\.");  
    // var dec = sizestr.substr(len + 1, 2);
    // if(dec == "0"){//当小数点后为00时 去掉小数部分  
    //     return sizestr.substring(0,len) + sizestr.substr(len + 3,2);  
    // }  
    // return sizestr;  
} 
// 单位格式化
export function converunit(limit){
    limit = limit * 1;
    var size = "";
    if (limit < 1024) {
        size = 'KB';
    } else if (limit < 1024 * 1024) {  
        size = 'MB';
    } else if (limit < 1024 * 1024 * 1024) {  
        size = "GB";  
    } else {
        size = "TB";  
    }
    return size;  
} 
// 颜色转换
export function colorRgb(color){
    var color = color.toLowerCase();
    var pattern = /^#([0-9|a-f]{3}|[0-9|a-f]{6})$/;
    if(color && pattern.test(color)) {
        if(color.length == 4) {
            // 将三位转换为六位
              color = '#' + color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
        }
        //处理六位的颜色值
        var colorNew = [];
        for (var i=1; i<7; i+=2) {
            colorNew.push(parseInt("0x"+color.slice(i, i+2)));
        }
        return colorNew.join(",");
    }
    return color;
}